So previously, we've discussed the accumulator pattern,
but the accumulator pattern becomes even more
powerful when recombine it with conditionals.
So, recall that the accumulator pattern involves three broad steps.
So first, we initialize
an accumulator variable and then,
we iterate through the relevant sequence,
and then as we iterate through that sequence,
we update our accumulator variable.
Then in the end,
the accumulator variable, if we did these steps right has the answer that we want.
Okay. So, let's apply this template and use it in combination with conditionals.
So, suppose that we have a phrase here and
we want to count the number of characters that are not spaces.
So in other words, we want to count one, two, three, four,
you want to skip the space, five,
skip the space, six,
seven, and so on.
Then, we can use the accumulator pattern with conditionals.
The way that we can do that is here,
we initialize our accumulator variable total to be zero,
in other words, the number of non-space characters that we've seen so far.
Then, we iterate through our sequence in this case it's phrase and then,
we update our accumulator variable appropriately.
So in this case, we want to say,
we've seen one more non-character space if the character in our phrase is not a space.
So we write, if character and then exclamation point equals that's to say not equal to.
So, if character is not a space then we say,
total equals total plus one.
So, when we run our code,
it's going to count the number of non-space characters.
In this case, it's 26.
If we had only spaces here,
then we would get zero.
If we had three non-space characters,
then we would get three and so on.
If we wanted to solve a slightly different problem,
so let's suppose that we want to count the number of vowels inside of this string s,
then we take the same broad steps.
So, we initialize our accumulator variable x to be zero,
then we loop through our sequence,
and then we say, if this character i,
so i is the individual character,
so if it's in this list a,
e, i, o, u,
so in other words this is saying,
if i is a vowel,
it's one of a, e,
i, o or u.
If it's a vowel, then we update our accumulator variable x to add one onto it,
and so the result is that we're going to count the number of vowels.
So, when we run our code,
we can see that there are eight vowels in the string s. So,
we can visualize this with an example.
So here, we want to cut the number of o's in onomatopoeia.
In our code, we loop through every character and we
say if the character c is the character o,
then add one to o count.
The result is that we're going to loop through every character in our word,
and print out the number of o's in our word.
So, when we see the first o,
we add one onto it.
When see the second o,
we add another onto it,
and then we keep looping through.
Every time we see an o,
we increment o count by one,
and when our code is done running, then,
we o count would be four,
and we print out there are four o's in onomatopoeia.
Another common pattern that mixes conditionals in
the accumulator pattern is what's called max value accumulation.
So, in max value accumulation,
we're trying to find the largest thing in a list of things.
So, in this example,
suppose that we want to find the largest number in this list of numbers.
So, nums is just a random list of numbers.
What we're going to do is we're going to use
the accumulator pattern to keep track of the
best or highest number that we've seen so far.
So, in other words, we assign best_num.
So at first, we just assign it to be the first number that we see.
So, we assign best_num to be nine and then
what we're going to do is we're going to loop through all of the numbers in nums and say,
if we find a higher number than our best numbered so far,
then we're going to update best num to be that number.
So, we say for every number n,
if n is greater than the best number that we've seen so far,
then reassign best_num to be n. So,
I'm going to simulate execution on this code before actually running it.
Okay. So first, we assign best_num to be the first number,
then we loop through every number.
So, after we assign best_num to be the first number,
then we loop through every number.
So first, n is going to be nine and we
say is nine greater than best num which is nine? It's not.
So, we then loop through you to the next number three.
Is three greater than nine?
It's not. So, we loop through to the next number eight.
Is eight greater than nine? It's not.
So, we loop through to the next number 11 and then now,
when n is 11, we say,
is 11 greater than nine? It is.
So, we reassign best_num from nine to instead be 11 and then,
we loop through to the next number five
and rather than comparing it to nine because best_num is now 11,
we compare, is five greater than 11?
It is not. So, we loop through to the next number 29 and
we ask is 29 greater than 11? It is.
So, we reassign best_num to be 29 and then,
after we've done that,
n is now two and we're asked is two greater than 29?
It's not. So, at that point,
because we've gone through all the numbers in our sequence,
then we're done with this for-loop.
When we're done with this for-loop,
best_num has been assigned to 29 and so,
we print out 29.
So in other words, our strategy for
max value accumulation is to keep track of the highest value that we've seen so far,
and then to loop through every item in the sequence,
and if we see a number higher than the highest one that we've seen so far,
then reassign our highest that we've seen so far to that higher number.
We know that by the time this for-loop is done running,
then best_num, it's going to be the largest number that's in our sequence.
So, when you run this code,
we should expect 29 to print out and we see that it does.
If I change this sequence to instead have 50 instead of five,
then we should now expect 50 to print out and so on.
If I change this to be 200,
200 would print out.
So, it always prints out the largest number in this sequence.
So, in this question, we're asked what is printed by the following statements?
So, we assign s to the string we are learning,
then we assign x to be zero.
So, x here is our accumulator variable,
s is the sequence that we're iterating over and we say,
if i is in a, b,
c, d, e. So,
in other words, if it's one of these characters,
then assign x to be x plus one,
then at the end, we print out what's the value of x.
So, this is the accumulation pattern and we're
accumulating the number of characters that are a,
b, c, d and e. So,
in order to answer this question,
we have to count the number of a, b, c,
d's and e's in the string s. So I see, 1, 2,
3, 4, 5 and so,
I expect the answer to be five.
So, this question asks,
what's printed by the following statements?
So, we have a variable called min value that gets
assigned to zero and we have a list that we're iterating over in this for-loop,
and we say, if an item in this list is less than min value,
then min value equals item.
So here, we assign min value to zero,
and one thing I noticed right away is that
no item in this list is actually less than zero.
So, what that means is that this min expression is never
going to be true because we don't have an item that's less than zero.
So, I expect this to never run and by the end of our loop,
min value is going to be zero.
So, if we actually wanted to collect the minimum value in this list,
rather than assigning min value to be zero,
we should assign min value equals the first item in the list.
So another words, list sub zero,
and then min value would start out as five,
and we would do a form of max value accumulation.
So, as we've seen,
the accumulator pattern becomes even more powerful when we combine it with conditionals.
That's all for now until next time.